#ifndef _DYNAMIC_IMAGE_H_
#define _DYNAMIC_IMAGE_H_ 

#include "stdafx.h"

class DynamicImage
{
public:
	DynamicImage(Ogre::SceneManager* _mSceneMgr, 
		Ogre::String name, 
		Ogre::Camera* _mCamera, 
		Ogre::Vector3 _nodePosition, 
		Ogre::Vector3 _drawColor, 
		Ogre::Vector2 _imageSize, 
		bool _drawable, 
		bool _controllable, 
		int _sn);
	~DynamicImage(void){
		mImageSrc.freeMemory();
		mImageDst.freeMemory();
		if (mSceneMgr->hasManualObject(manualObjectName))
			mSceneMgr->destroyManualObject(manualObjectName);
		Ogre::MaterialManager::getSingleton().remove(materialName);
		Ogre::TextureManager::getSingleton().remove(tempImageStr); 
		mSceneMgr->getSceneNode(nodeName)->removeAndDestroyAllChildren();
		mSceneMgr->destroySceneNode(nodeName);
	};
	void updateBuffer(unsigned char* data);
	void updateBuffer(unsigned char* data, int channelNum);
	void createImage(int imWidth, int imHeight, unsigned char* img_data, Ogre::PixelFormat pf);
	void halfLuminance();
	void doubleLuminance();
	Ogre::Vector3 getPosition();
	void nodeMoved(Ogre::Vector3 newPosition);
	void nodeScale(Ogre::Vector2 newSize);
	void setRenderingQueue( int queueID );
	void openImage( const char* filePath );
	
	void saveImage(const char *filename );
	void renew();
	void mouseMoved(const OIS::MouseEvent &arg);
	int mousePressed(const OIS::MouseEvent &arg, OIS::MouseButtonID id);
	void getTouchPosition(const OIS::MouseEvent &arg);
	void mouseReleased(const OIS::MouseEvent &arg, OIS::MouseButtonID id);
	bool isTouched(const OIS::MouseEvent &arg, bool needDraw);
	void init();
	void reload();	
	void createImage(int imWidth, int imHeight, unsigned char* img_data);
	void useSpecifiedTextures(Ogre::String _materialName);
	void useSpecifiedTextures(Ogre::String _materialName, Ogre::String _textureName);
	void nodeRotated(Ogre::Vector2 _direction);
	bool drawable;
	bool controllable;

	Ogre::Vector2 imageRatio;
	Ogre::Vector2 drawPoint;

	Ogre::Image mImageSrc, mImageSrcSecond;
	Ogre::Image mImageDst;

	Ogre::String mPath, mPathSecond; // for recover image
	bool bAlt, bMidMouseDown, bLMouseDown, bRMouseDown;	//true if mouse buttons are held down

	unsigned char* img_data,*original_data;
	int img_width;
	int img_height;

	Ogre::SceneManager* mSceneMgr;
	Ogre::Camera* mCamera;
	Ogre::Viewport *mCamView;

	Ogre::Vector3 drawColor;
	Ogre::Vector3 nodePosition;
	//Ogre::Vector3 nodeScale;
	Ogre::Vector2 imageSize;

	Ogre::String nodeName;
	Ogre::String materialName;
	Ogre::String manualObjectName;
	Ogre::String tempImageStr;

	enum {bb, gg, rr, aa}; 
	int sn;

	std::vector<Ogre::Vector2> points;
};

#endif	//DYNAMIC_IMAGE_H_